﻿////////////////////////////////////////////////////////////////////////////////
// Source Copyright (C) J.Fan
// @author      J.Fan
// @date        2013-01-19
// @revision    Revision in Version Control System
// @remark      
////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using JsonConverter.Converter;
using JsonConverter.Writer;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestJsonConverter
{
    [TestClass]
    public class UnitTestStringConverter
    {
        [TestMethod]
        public void Test()
        {
            Assert.IsTrue(new int[0] is IEnumerable);
            Assert.IsTrue(new int[0] is IEnumerable<int>);
            Assert.IsTrue(typeof(IEnumerable).IsAssignableFrom(typeof(IEnumerable<int>)));
        }

        [TestMethod]
        public void TestConvertString()
        {
            StringConverter convert = new StringConverter();
            Assert.AreEqual(StringConverter.EMPTY, convert.Convert(string.Empty));
            Assert.AreEqual("\"a\\u0019bcde\\nfghijk\\t\"", convert.Convert("a\x0019bcde\nfghijk\t"));
        }

        [TestMethod]
        public void TestConvertDateTime()
        {
            DateTime value = new DateTime(2013, 1, 19, 12, 34, 56);

            Assert.AreEqual("1358570096000",
                new DateTimeMillisecondConverter().Convert(value));

            Assert.AreEqual("\"\\/Date(1358570096000+0800)\\/\"",
                new DateTimeMicrosoftConverter().Convert(value));

            Assert.AreEqual("\"2013-01-19 12:34:56\"",
                new DateTimeFormatConverter().Convert(value));

            DateTimeFormatConverter c = new DateTimeFormatConverter("yyyyMMdd HHmmss");
            Assert.AreEqual("{0:yyyyMMdd HHmmss}", c.FormatPattern);
            Assert.AreEqual("\"20130119 123456\"",
                new DateTimeFormatConverter("yyyyMMdd HHmmss").Convert(value));

            Assert.AreEqual("\"Sat Jan 19 2013 12:34:56 +0800\"", new DateTimeJavaScriptConverter().Convert(value));
        }

        [TestMethod]
        public void TestEnumerable()
        {
            StringBuilder builder = new StringBuilder();
            StringBuilderJsonWriter writer = new StringBuilderJsonWriter(builder);
            EnumerableConverter c = new EnumerableConverter();
            c.Convert(writer, new int[] { 0, 1, 2, 3, 4, 5, 6 });
            Assert.AreEqual("[0,1,2,3,4,5,6]", builder.ToString());
        }
    }
}
